package com.tanger.permission.permission.web;

import com.tanger.common.exception.AbstractController;
import com.tanger.common.result.ActionResult;
import com.tanger.common.utils.StringUtil;
import com.tanger.permission.entity.role.EntityRole;
import com.tanger.permission.entity.vo.RoleQueryVo;
import com.tanger.permission.entity.vo.RoleUpdateVo;
import com.tanger.permission.permission.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 控制层
 */
@RestController
@RequestMapping("/tanger/bts/role")
@Api(value = "角色",tags="角色")
public class RoleController extends AbstractController {

	@Autowired
	private RoleService roleService;

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "Long")
	@ApiOperation(value = "权限详情")
	@GetMapping("/{id}")
	public ActionResult<EntityRole> getInfo(@PathVariable Long id) {
		try {
			if (StringUtil.isNull(id)) return ActionResult.buildResultError("参数缺失");
			return ActionResult.buildResultSuccess(roleService.findRoleById(id));
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
	}


	@ApiOperation(value = "查询角色列表")
	@PostMapping(value = "/roleList")
	public ActionResult<EntityRole> roleList(@RequestBody RoleQueryVo queryVo) {
		try {
			return ActionResult.buildResultSuccess(roleService.findAll(queryVo));
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
	}

	@ApiOperation(value = "保存角色")
	@PostMapping(value = "/saveRole")
	public ActionResult saveRole(@RequestBody EntityRole role) {
		try {
			if (StringUtil.isNull(role.getRoleCode()) || StringUtil.isNull(role.getRoleName()))
				return  ActionResult.buildResultError("参数缺失");
			return roleService.saveRole(role);
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
	}

	@ApiOperation(value = "设置角色状态(启用、禁用、删除)")
	@PostMapping(value = "/updateStatus")
	public ActionResult handleRoleInfo(@RequestBody RoleUpdateVo updateVo) {
		try {
			if (CollectionUtils.isEmpty(Arrays.asList(updateVo.getIds())) || StringUtil.isNull(updateVo.getStatus()))
				return  ActionResult.buildResultError("参数缺失");
			return roleService.batchUpdateRole(updateVo);
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
	}
}